package com.lanou.sinasports.utils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
 * Created by dllo on 16/1/13.
 * 单例模式
 */
public class VolleySingleton {
//    private static class SingleHolder{
//        private static final VolleySingleton instance=new VolleySingleton();
//    }
//    private static VolleySingleton getInstance(){
//        return SingleHolder.instance;
//    }

    private RequestQueue queue;
    private ImageLoader imageLoader;
//    private static VolleySingleton instance;
//      public static VolleySingleton getInstance(){
//          if(instance==null){
//              instance=new VolleySingleton();
//          }
//          return instance;
//      }
//    private VolleySingleton() {
//    queue = getQueue();
//    imageLoader = new ImageLoader(queue, new BitmapCache());
//
//    }

    //双重锁
    private volatile static VolleySingleton instance;
    private VolleySingleton(){
        queue = getQueue();
        imageLoader = new ImageLoader(queue, new BitmapCache());

    }
    public static VolleySingleton getInstance() {
        if (instance == null) {
            synchronized (VolleySingleton.class) {
                if (instance == null) {
                    instance = new VolleySingleton();
                }

            }
        }
        return instance;
    }




//    private VolleySingleton() {
//        queue = getQueue();
//        imageLoader = new ImageLoader(queue, new BitmapCache());
//    }
//
//
//    private static final class SingletonHolder {
//        private static final VolleySingleton instance = new VolleySingleton();
//    }
//
//    public static VolleySingleton getInstance() {
//
//        return SingletonHolder.instance;
//
//    }


    public static final String TAG = "VolleySingleton";
    private RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(LessonApplication.getContext());
        }
        return queue;
    }



    public <T> void _addRequest(Request<T> request) {
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void _addRequest(Request<T> request, Object tag) {
        request.setTag(tag);
        queue.add(request);
    }

    public void _addRequest(String url, Response.Listener<String> listener,
                            Response.ErrorListener error) {
        StringRequest stringRequest = new StringRequest(url, listener, error);
        _addRequest(stringRequest);
    }

    public <T> void _addRequest(String url, Class<T> clazz, Response.Listener<T> success, Response.ErrorListener failed) {
        GsonRequest<T> request = new GsonRequest<T>(url, clazz, success, failed);
        _addRequest(request);
    }

    public void removeRequest(Object tag) {
        queue.cancelAll(tag);
    }

    public ImageLoader _getImageLoader() {
        return imageLoader;
    }

    public static void addRequest(String url, Response.Listener<String> listener,
                                  Response.ErrorListener error) {
        getInstance()._addRequest(url, listener, error);
    }

    public static <T> void addRequest(String url, Class<T> clazz, Response.Listener<T> success, Response.ErrorListener failed) {
        getInstance()._addRequest(url, clazz, success, failed);
    }

    public static ImageLoader getImageLoader(){
        return getInstance()._getImageLoader();
    }


}
